package com.hogan.hoganproject.base.activity;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.hogan.hoganproject.base.app.HoganApplication;
import com.hogan.hoganproject.base.mvp.BasePresenter;
import com.hogan.hoganproject.base.utils.SharedPreferencesUtils;

/**
 * Author: chenhu
 * Time: 2020/8/28 15:39
 * Description do somethings
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected Context mContext;
    protected SharedPreferencesUtils mSharedPreferencesUtils;
    protected FragmentManager mFragmentManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getLayoutResID() == 0) {
            throw new NullPointerException("layout is not empty");
        }
        mContext = this;
        setContentView(getLayoutResID());
        ActivityUtil.addActivity(this);
        mSharedPreferencesUtils = SharedPreferencesUtils.getInstance(HoganApplication.getInstance()
                .getApplicationContext());
        mFragmentManager = getSupportFragmentManager();
        initActivity();

    }

    public abstract int getLayoutResID();

    protected abstract void initActivity();


    protected void addFragment(@NonNull Fragment fragment, int frameId, String tag) {
        if (mFragmentManager != null) {
            ActivityUtil.addFragmentToActivity(mFragmentManager, fragment, frameId, tag);
        }
    }

    protected void replaceFragment(@NonNull Fragment fragment, int frameId, String tag) {
        if (mFragmentManager != null) {
            ActivityUtil.replaceFragmentToActivity(mFragmentManager, fragment, frameId, tag);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityUtil.finishActivity(this);
    }
}
